package com.shunbo.convenient.controller;

import com.shunbo.convenient.service.MenuService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.Menu;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author weizhuang
 * 地址
 */
@RestController
@RequestMapping("/menu")
public class MenuController {


    @Resource
    private MenuService menuService;


    /**
     * 菜单列表
     *
     * @param menu 菜单
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getMenuList(@RequestBody Menu menu) {
        try {
            List<Menu> all = menuService.findAll(menu);
            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 添加菜单
     *
     * @param menu 菜单
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveMenu(@RequestBody Menu menu) {
        try {
            Integer save = menuService.save(menu);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 更新菜单
     *
     * @param menu 菜单
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateMenu(@RequestBody Menu menu) {
        try {
            Integer save = menuService.update(menu);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 删除菜单
     *
     * @param menu 菜单
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteMenu(@RequestBody Menu menu) {
        try {
            Integer save = menuService.delete(menu.getId() + "");
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * id查菜单
     *
     * @param menu 菜单
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult getMenuById(@RequestBody Menu menu) {
        try {
            Menu st = menuService.findById(menu.getId() + "");
            return ConvenientResult.build(200, "成功", st);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }



}
